'''
Created on Dec 13, 2011

@author: aren1
'''

import time
import thread
import threading
import monitor
import interface

attackKey = 0x30
attackKey1 = 0x37
restKey = 0x39
bloodKey = 0x38
suiKey = 0x34
hutiKey = 0x35

minBlood = 2000
isRest = False

lock = thread.allocate_lock()
con = threading.Condition() 

suiTime = 0
hutiTime = 0
attackCount = 0
normalAttackCount = 0

def doWork():
    while True:
        doPause()
        doRest()
        doBlood()
        doAttack()

def doPause():
    if monitor.isPaused() == True:
        con.acquire()
        con.wait()
        con.release()
        
def notify():
    con.acquire()
    con.notify()
    con.release()
    
def updateRest(status):
    global isRest
    lock.acquire()
    isRest = status
    lock.release()

def isResting():
    global isRest
    return isRest

def doBlood():
    global isRest
    if isRest == False:
        if interface.getBlood() < minBlood:
            interface.pressAt(bloodKey)  
               
def doAttack():
    global isRest
    global normalAttackCount
    if isRest == False:
        attack()
        normalAttackCount = normalAttackCount + 1
        if normalAttackCount >= 6:
            interface.pressAt(attackKey1)
            normalAttackCount = 0

def attack():
    interface.pressAt(attackKey)
    time.sleep(1)
    interface.pressAt(attackKey)
    print "attack"
        
def doRest():
    global isRest
    global attackCount
    if isRest == True:
        if interface.getBlood() == interface.getMaxBlood():
            updateRest(False)
            attackCount = 0
        else:
            if attackCount < 5:
                attack()
                attackCount = attackCount + 1
            else:
                time.sleep(10)
                interface.pressAt(restKey)